package hong.jun._01_hello;


import com.rabbitmq.client.*;
import hong.jun.ConnectionUtil;

import java.io.IOException;

//消息接收方
public class Rev {

    public static void main(String[] args) throws Exception {
        //1.创建链接
        Connection connection = ConnectionUtil.getConnection();
        //2.创建通道
        final Channel channel = connection.createChannel();
        //3.声明队列
        channel.queueDeclare(send.NAME_QUEUE_HELLO_1010,
                true,
                false,
                false,
                null
        );

        //拿到消息之后的回调，用来消费消息的。
        Consumer consumer = new DefaultConsumer(channel) {
            /**匿名内部类 相当于将DefaultConsumer的方法复写后再实例化*/
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
                    throws IOException {
                //模拟消费方法故障
                //int i = 1 /0;
                System.out.println("消费者的标签：" + consumerTag);
                System.out.println("队列RoutingKey：" + envelope.getRoutingKey());
                System.out.println("交换机Exchange：" + envelope.getExchange());
                System.out.println("消息的ID：" + envelope.getDeliveryTag());
                System.out.println("消息的内容：" + new String(body));

                //触发手动签收
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        };

        //4.消费消息 ： 队列名 ， 签收方式 (true，false)， 消费回调
        //拿到消息只有 ，自动签收，MQ会把该消息删除
        //channel.basicConsume(Send.NAME_QUEUE_HELLO_1010 , true,consumer);
        channel.basicConsume(send.NAME_QUEUE_HELLO_1010, false, consumer);

        //connection.close();
        /**
         * consumer
         如同：回调函数
         var met =  function(data){
         };
         $.post("/user/add",{},  met );
         **/
    }
}
/**
 * class Temp extends  DefaultConsumer{
 * public Temp(Channel channel) {
 * super(channel);
 * }
 *
 * @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
 * throws IOException {
 * System.out.println("消息的内容："+new String(body));
 * }
 * }
 **/